package com.zeros.mysplash.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.zeros.mysplash.R;
import com.zeros.mysplash.UserCenter;
import com.zeros.mysplash.activity.LoginActivity;
import com.zeros.mysplash.activity.WebActivity;
import com.zeros.mysplash.bean.Splash;
import com.zeros.mysplash.common.Constants;
import com.zeros.mysplash.http.service.SplashDownLoadService;
import com.zeros.mysplash.utils.SerializableUtils;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

/**
 * 复制的某某的[具体忘了]，
 */
public class SplashActivity extends AppCompatActivity {

    private Button mSpJumpBtn;
    private ImageView mSpBg;
    private Splash mSplash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        this.getWindow().setFlags(flag, flag);

        setContentView(R.layout.activity_splash);
        mSpJumpBtn = findViewById(R.id.sp_jump_btn);
        mSpBg = findViewById(R.id.sp_bg);
        checkSDCardPermission();        //检查权限
        showAndDownSplash();            //显示图片
    }

    /**
     * 显示图片
     */
    private void showAndDownSplash() {
        showSplash();
        startImageDownLoad();
    }

    //下载图片--【重点】
    private void startImageDownLoad() {
        SplashDownLoadService.startDownLoadSplashImage(this, Constants.DOWNLOAD_SPLASH);
    }

    //先显示内存图片
    private void showSplash() {
        mSplash = getLocalSplash();

        if (mSplash != null && !TextUtils.isEmpty(mSplash.getSavePath())) {
            Log.e("SplashDemo", "SplashActivity 获取本地序列化成功" + mSplash);
            Glide.with(this)
                    .load(mSplash.getSavePath())
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(mSpBg);

            startClock();           //倒计时开始

        } else {
            mSpJumpBtn.setVisibility(View.GONE);
            mSpJumpBtn.postDelayed(new Runnable() {
                @Override
                public void run() {
                    gotoLoginOrMainActivity();
                }
            }, 1000);
        }
    }

    //从内存获取图片地址
    private Splash getLocalSplash() {
        Splash splash = null;

        try {
            Log.e("Tag-存储路径", Constants.SPLASH_PATH);//修改为存储到内存卡中，不需要动态申请权限
            // /data/user/0/com.zeros.mysplash/files/my/splash
            File serializableFile = SerializableUtils.getSerializableFile(Constants.SPLASH_PATH, Constants.SPLASH_FILE_NAME);
            splash = (Splash) SerializableUtils.readObject(serializableFile);

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Tag-SplashDemo", "SplashActivity 获取本地序列化闪屏失败" + e.getMessage());
        }
        return splash;
    }

    //倒计时开始
    private void startClock() {
        mSpJumpBtn.setVisibility(View.VISIBLE);
        countDownTimer.start();
    }


    //检查权限
    private void checkSDCardPermission() {

    }

    //倒计时
    //由于CountDownTimer有一定的延迟，所以这里设置3400
    private CountDownTimer countDownTimer = new CountDownTimer(5200, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mSpJumpBtn.setText("跳过(" + millisUntilFinished / 1000 + "s)");
        }

        @Override
        public void onFinish() {
            mSpJumpBtn.setText("跳过(" + 0 + "s)");
            gotoLoginOrMainActivity();
        }
    };


    //点击
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sp_bg:
                Log.e("Tag--", "ImageView需要添加ckickable");
                gotoWebActivity();
                break;
            case R.id.sp_jump_btn:
                gotoLoginOrMainActivity();
                break;
        }
    }

    //跳转到-广告详情页-WebActivity
    private void gotoWebActivity() {
        Log.e("Tag--000", mSplash.getClick_url());
        if (mSplash != null && mSplash.getClick_url() != null) {
            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("url", mSplash.getClick_url());
            intent.putExtra("title", mSplash.getTitle());
            intent.putExtra("fromSplash", true);
            intent.putExtra("needShare", false);
            startActivity(intent);
            finish();
        }
    }


    //跳转
    private void gotoLoginOrMainActivity() {
        countDownTimer.cancel();
        if (UserCenter.getInstance().getToken() == null) {
            gotoLoginActivity();
        } else {
            gotoMainActivity();
        }
    }

    private void gotoLoginActivity() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void gotoMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0,0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

}
